<?php

namespace App\Http\Controllers\Admin;

use App\Models\RedBag;
use App\Models\SystemConfig;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;
class RedBagController extends Controller
{
    public function index(Request $request)
    {
        $serach = [];
        $key    = '';
        $val    = '';
        $input= $request->all();
        if (!empty($input)){
            $key           = key($input);
            $val           = current($input);
            $serach['key'] = $key;
            $serach['val'] = $val;
        }
        $data               = RedBag::getRedBagList($key,$val);
        $info               = RedBag::getRedBagCount();
        $red_bag_conf       = SystemConfig::getConfValByConfKey('red_bag_total_amount');
        return view('admin.red_bag.index',['lists'=>$data,'search'=>$serach,'count'=>$info['count'],'total'=>$info['total'],'red_bag_conf'=>$red_bag_conf]);
    }

    public function edit($amount)
    {
        if (!is_numeric($amount)||$amount<0){
            return redirect('admin/RedBag/index')->with('danger','更新失败，输入不合法');
        }
        $amount = sprintf("%.2f",$amount);
        $flag   = SystemConfig::modifyValByConfKey('red_bag_total_amount',$amount);

        if($flag){
            return redirect()->back()->with('success','修改成功');
        }else{
            return redirect()->back()->with('danger','修改失败');
        }
    }

}
